<?php

/**
 * Controller_manage
 * 各个管理
 */

class Controller_article extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->user = YOU::loadModel('user');

	}

    /** 首页 **/
    public function actionIndex()
    {

    }
   
       /** 文章列表页 **/
    public function actionArticleList ()
    {   
		//搜索
		if($_POST){
			$article_title     	    = $this->post->article_title;
			$article_author     	= $this->post->article_author;
			$start_time     	    = $this->post->start_time;
			$end_time     			= $this->post->end_time;
			// 通过标题搜索
			if (!empty($article_title)){

				$article_lists = $this->db->fetchAll("SELECT * FROM `tbl_article` WHERE article_title like '%{$article_title}%'");
				
			}
			// 通过提交者搜索
			if(!empty($article_author)){
			
			}
			// 通过时间段搜索
			if(!empty($start_time)||!empty($end_time)){

				$article_lists = $this->db->fetchAll("SELECT * FROM `tbl_article` WHERE create_time>='{$start_time}' AND create_time<='{$end_time}+1'");
			
			}
						
		}else{
    	 // 分页     
        YOU::loadLib('page', "", '0');
        $tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_article`");
        $count = $tmp['count'];
        $page_total = ceil($count/PAGE_LIMIT);
        // 当前页                  
        $p = intval($_GET['p']);
        if ($p == 0){
            $p  = 1;
        }
        if ($p > 1){
            $start = PAGE_LIMIT * ($p-1);
        }else{
            $start = 0;
        }
        $article_lists = $this->db->fetchAll("SELECT * FROM `tbl_article` ORDER BY article_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

        
        //时间日期转换 
        foreach($article_lists as $key=>$value){
              $article_lists[$key]['create_time'] = date("Y-m-d", strtotime($article_lists[$key]['create_time']));
              $article_lists[$key]['staff_name']  = $this->model->user->getNameById($value['user_id']);
        }
		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
        $page->next_page   = '下一页>';
        $page->pre_page    = '<上一页';
        $page->style_path  = "static/page_style";
		
        $this->tpl->assign("page", $page->show(4, "yahoo"));
		}
        $this->tpl->assign("article_lists", $article_lists);
        $this->tpl->display('article/article.list.tpl.php');
    } 
    
   
     /** 添加文章页 **/
    public function actionArticleAdd()
    {        
        if ($_POST){ 

            $article_title     	    = $this->post->article_title;
            $article_content   		= $this->post->article_content;    
            $article_file   		= $this->post->article_file;            

            // 写入数据库
            $info = array( 
			"user_id"  	            => $this->user['user_id'],				
			"article_title"  	    => $article_title,
            "article_content"      	=> $article_content,  
            "article_file"       	=> $article_file,                    
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00", 
				
			 );
          $this->db->do_insert("tbl_article", $info);
          Helper_Client::location("添加成功", "index.php?a=article.articlelist");
         }
          
         $this->tpl->display("article/article.add.tpl.php");
    } 
    
      /** 修改文章页 **/
    public function actionArticleUpdate()
    {   

        $article_id   = (int)$this->get->article_id;
        $article_data = $this->db->fetchRow("SELECT `article_title`,`article_content`,`article_file` FROM `tbl_article` WHERE `article_id`='{$article_id}'");
	
	    if (empty($article_data)){
            Helper_Client::alert("文章不存在");
        }

        if ($_POST){
			
            $article_title     	    = $this->post->article_title;
            $article_content   		= $this->post->article_content;    
            $article_file   		= $this->post->article_file;            

            // 写入数据库
            $info = array(
				
			"article_title"  	    => $article_title,
            "article_content"      	=> $article_content,  
            "article_file"       	=> $article_file,                    
            "status"                => 1,
            "create_time"           => date("Y-m-d H:i:s"),
            "update_time"           => "0000-00-00 00:00:00",
				
			 );
          $this->db->do_update("tbl_article", $info, "article_id='{$article_id}'");
          // 跳转
          Helper_Client::location("修改成功", "index.php?a=article.articlelist");
         }
            $this->tpl->assign("article_data", $article_data);
	        $this->tpl->display("article/article.update.tpl.php");
    } 
    
     /** 文章详情页 **/
    public function actionArticleInfoList ()
    {   
    	$article_id   = (int)$this->get->article_id;
        $article_data = $this->db->fetchRow("SELECT * FROM `tbl_article` WHERE `article_id`='{$article_id}'");
	    if (empty($article_data)){
            Helper_Client::alert("文章不存在");
        }
        //阅读次数
       $this->db->fetchRow("UPDATE `tbl_article` SET `article_pc`= `article_pc`+1 WHERE `article_id`='{$article_id}'");
       $article_pc = $this->db->fetchRow("SELECT `article_pc` FROM `tbl_article` WHERE `article_id`='{$article_id}'");
        //发布部门
       $department_data = $this->db->fetchRow("SELECT `department_name` FROM `tbl_department` WHERE `manage_id`='{$manage_id}'");
       //部门和人名罗列
       $department_lists = $this->db->fetchAll("SELECT d.`department_name`,s.`staff_name` FROM `tbl_department` AS d,`tbl_staff` AS s WHERE d.`department_id`=s.`department_id`");
        //时间日期转换 
       $manage_data['create_time']= date("Y-m-d", strtotime($manage_data['create_time']));
       $this->tpl->assign("article_pc", $article_pc);
       $this->tpl->assign("department_data", $department_data);
       $this->tpl->assign("department_lists", $department_lists);
       $this->tpl->assign("article_data", $article_data);
       $this->tpl->display('article/article.info.tpl.php');
    } 
    
    
    /** 文章删除 **/
    public function actionArticleDel()
    {
        $article_id = $this->get->article_id;   
        $this->db->query("DELETE  FROM `tbl_article` WHERE article_id='{$article_id}'");
         // 跳转
        Helper_Client::location("删除成功", "index.php?a=article.articlelist");
    }

                           
}

?>